const { app, BrowserWindow, Menu, session, globalShortcut } = require('electron')
const { join } = require('path')
const path = require('path')
//const preloadPath = app.isPackaged ? "../../preload.js" : "../preload.js";
const preloadPath = './preload.js'
const renderProcessApi = path.resolve(__dirname, preloadPath)
// process.env.DIST = join(process.env.DIST_ELECTRON, "../dist");
// const indexHtml = join(process.env.DIST, "index.html");
process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = true
const createWindow = async () => {
  Menu.setApplicationMenu(null)
  const win = new BrowserWindow({
    // width: 1024,
    // height: 768,
    width: 1040,
    height: 807,
    fullscreen: false, //全屏
    title: '虚拟评标室',
    // frame: false, //直接去除导航头部
    show: true,
    webPreferences: {
      webSecurity: false,
      nodeIntegration: true,
      enableRemoteModule: true,
      contextIsolation: false,
      preload: renderProcessApi
    }
  })
  // win.loadFile(join(__dirname, "../dist/index.html"));
 
  const env = app.isPackaged ? 'production' : 'development'
  console.log('env ' + env)
  console.log('process.env.NODE_ENV ' + process.env.VITE_APP_ENV)
  const indexHtml = {
    development: 'http://localhost:5173', // 开发环境
    // development: join(__dirname, "../dist/index.html"), // 开发环境
    production: join(__dirname, '../dist/index.html#/') // 生产环境
  }
 
  if (app.isPackaged) {
    win.loadFile(indexHtml[env])
  } else {
    win.loadURL(indexHtml[env])
  }
  globalShortcut.register('Ctrl + Shift + i', function () {
    win.webContents.openDevTools()
  })
  globalShortcut.register('f11', function () {
    if (win.isFullScreen()) {
      win.setFullScreen(false)
    } else {
      win.setFullScreen(true)
    }
  })
}
app.whenReady().then(async () => {
  // if (!app.isPackaged) {
  //   await session.defaultSession.loadExtension(
  //     join(__dirname, "../plugins/vuetools6.6.1_0")
  //   );
  // }
  createWindow()
  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) createWindow()
  })
})
app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') app.quit()
})